{extend name="public/list_page"/}
{block name="content"}
<div class="layui-card">
    <div class="layui-card-body">
        <table class="layui-table" id="employeeList" lay-filter="list"></table>
    </div>
</div>
{/block}

{block name="custom"}
<script type="text/html" id="toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="add"><i class="layui-icon">&#xe654;</i>添加</button>
    </div>
</script>

<script type="text/html" id="barHandle">
    <button type="button" class="layui-btn handel-btn layui-btn-primary" lay-event="edit" title="编辑"><i class="fa fa-pencil-square-o"></i></button>
    <button type="button" class="layui-btn handel-btn layui-btn-del" lay-event="del" title="删除"><i class="fa fa-trash-o"></i></button>
</script>

<script type="text/html" id="imgTmp">
    {{# if(d.photo){ }}
    <img src="{{d.photo}}" width="100" height="100">
    {{#  } }}
</script>

<script type="text/html" id="isDel">
    <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="显示|隐藏" lay-filter="isDel" {{ d.status == 1 ? 'checked' : '' }}>
</script>

<script type="text/html" id="isRecommended">
    <input type="checkbox" name="is_recommended" value="{{d.id}}" lay-skin="switch" lay-text="推荐|推荐" lay-filter="isRecommended" {{ d.is_recommended == 1 ? 'checked' : '' }}>
</script>
{/block}

{block name="js"}
<script>
    layui.use(['table','form'], function() {
        var table = layui.table,
            form = layui.form

        var employeeList = table.render({
            title: '',
            elem: "#employeeList",
            method: "post",
            url: "{:url('employee/index')}",
            page: true,
            limits: [10, 25, 50, 100, 200, 300, 400, 500, 1000],
            toolbar: "#toolbar",//开启头部工具栏
            defaultToolbar:[],
            response: {
                statusCode: 'suc'
            },
            cols: [[
                // {type:'checkbox'},
                {field: 'name', title: '姓名', align: 'center'},
                {field: 'photo', title: '照片', align: 'center',templet: "#imgTmp"},
                {field: 'sort', title: '排序', align: 'center'},
                {field: 'is_recommended', title: '推荐', align: 'center', templet: "#isRecommended"},
                {field: 'status', title: '状态', align: 'center', templet: "#isDel"},
                {field: '', title: '操作', align: 'center', toolbar: '#barHandle', width: 300}
            ]],
            done:function (res, curr, count) {
                employeeCurrPage = curr
                $('.layui-table-cell').css({'height':'auto'})
            }
        })

        //头部工具栏事件
        table.on('toolbar(list)',function (obj) {
            let action = obj.event
            switch (action) {
                case 'add':
                    xadmin.open('添加', '{:url("employee/add")}', 500, 650)
                    break;
            }
        })

        //单元格工具事件
        table.on('tool(list)',function (obj) {
            let action = obj.event
            let dataId = obj.data.id
            let url
            switch (action) {
                case 'edit':
                    url = "{:url('employee/edit')}?id=" + dataId
                    xadmin.open('编辑', url, 500, 650)
                    break;
                case 'del':
                    url = "{:url('employee/del')}"
                    layer.confirm('确认删除？', {title: ''}, function (index) {
                        ajaxRequest(url, {id: dataId}, function (res) {
                            if (res.code == 'suc') {
                                sucMsg(res.msg)
                            } else {
                                errMsg(res.msg)
                            }
                        })
                        layer.close(index)
                        employeeList.reload('employeeList', {page: employeeCurrPage})
                    })
                    break;
            }
        })

        form.on('switch(isDel)',function (obj) {
            let id = obj.value
            let tip = '';
            let type = 1;
            if (obj.elem.checked == true){
                tip = '确定显示？';
            } else {
                tip = '确定隐藏？';
                type = 2;
            }
            changeStatus("{:url('employee/changeStatus')}",id,type,1,tip,employeeList,'employeeList',employeeCurrPage)
        })

        form.on('switch(isRecommended)',function (obj) {
            let id = obj.value
            let tip = '';
            let type = 1;
            if (obj.elem.checked == true){
                tip = '确定推荐？';
            } else {
                tip = '确定取消推荐？';
                type = 2;
            }
            changeStatus("{:url('employee/changeStatus')}",id,type,2,tip,employeeList,'employeeList',employeeCurrPage)
        })
    });
</script>
{/block}